package com.shouhuyun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shouhuyun.constant.MessageConstant;
import com.shouhuyun.entity.Result;
import com.shouhuyun.pojo.Menu;
import com.shouhuyun.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态菜单
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService; // dubbo调用远程服务

    //查询所有菜单信息
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            //1. 调用服务层的 findAll方法
            List<Menu> menusIds = menuService.findAll();
            //1.1 成功 返回
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menusIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    // 动态菜单
    @RequestMapping("/dynamicMenu")
    public Result dynamicMenu(){

        try {
            // 1. 获得登录的用户信息
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            // 2. 获取用户姓名
            String username = user.getUsername();
            // 3. 查询 用户 可以访问的菜单
            List<Object> menuList = new ArrayList<>();
            menuList = menuService.findMenuByUserName(username);


            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }



}
